<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-5-16
 * Time: 下午10:24
 */
include "common.php";
class Interview extends Common{

    function __construct(){
        parent::__construct();
        $this->load->database();
    }


    /**
     * 明日之星
     */
    function star()
    {
        $data['star'] = $this->Marticle->getArticleAll(7,'10');
        $data['title'] = '微访谈-每日之星';
        $data['starMethod'] = 'day_star';
        $this->load->view('star',$data);
    }

    /**
     * 明日之星内页
     */
    function star_content()
    {
        $id = (int)$this->input->get('id');
        $uid = (int)$this->input->get('uid');

        if(!$id || !$uid){
            show_error('参数错误', 500 );
        }

        $data['star'] = $this->Marticle->getArticleById($id);
        $data['star_list'] = $this->Marticle->getArticleAll(7,'',5);

        $data['user'] = $this->Muser->getById ( $uid );
        $data['starMethod'] = 'day_star';
        $data['title'] = '每日之星内容页';
        $this->load->view('star_content',$data);
    }

    function ajaxVote()
    {
        $id = (int)$this->input->post('id');
        $interview = (int)$this->input->post('interview');
        $uid = isset($this->session->userdata ['id']) ? (int)$this->session->userdata ['id'] :0;

        if(!$uid){
            $data['statusCode'] = 2;
            $data['msg'] = '请先登陆';
            echo json_encode($data);
            exit;
        }

        if(empty($id)){
            $data['statusCode'] = 2;
            $data['msg'] = '参数错误';
            echo json_encode($data);
            exit;
        }

        $check = $this->Minterviewvote->checkVote($id,$uid,$interview);
        if($check){
            $data['statusCode'] = 2;
            $data['msg'] = '已投过票';
            echo json_encode($data);
            exit;
        }

        $this->Minterviewvote->insertVote($id,$uid,$interview);
        $this->Muser->voteInterview($id);

        $data['statusCode'] = 1;
        $data['msg'] = '成功';
        echo json_encode($data);

    }

    /**
     * 活动介绍
     */
    function intro()
    {
        $field='`id`,`title`,`column_id`,`create_time`,`content`';
        $data['star'] = $this->Marticle->getArticleIntro(15,'3','sort asc',$field);

        $data['title'] = '微访谈-活动介绍';
        $data['starMethod'] = 'intro';
        $this->load->view('intro',$data);
    }

    /**
     * 我要报名
     */
    function apply()
    {
        $id = isset($this->session->userdata ['id']) ? (int)$this->session->userdata ['id'] :0;

        $Muser = $this->Muser;
        $data['checkAlbum'] = $this->Malbum->checkCreateAlbum($id);
        $data['checkData'] = $Muser->checkUserDataIsPerFect($id);
        $data['userinfo'] = $Muser->getUserByUid($id);
        $data['weibo'] = $this->Mweibo->getWeiBoByUid($id,2);

        $data['title'] = '微访谈-我要报名';
        $data['starMethod'] = 'apply';
        $this->load->view('apply',$data);
    }

    function addApply()
    {
        $realname = $this->input->post('realname');
        $weixin = $this->input->post('weixin');
        $wid = $this->input->post('wid');
        $id = isset($this->session->userdata ['id']) ? (int)$this->session->userdata ['id'] :0;

        if(empty($id)){
            $data['statusCode'] = 2;
            $data['msg'] = '请先登陆';
            echo json_encode($data);
            exit;
        }
        $checkAlbum = $this->Malbum->checkCreateAlbum($id);
        $checkData = $this->Muser->checkUserDataIsPerFect($id);

        if(!$checkAlbum){
            $data['statusCode'] = 2;
            $data['msg'] = '您还未创建相册';
            echo json_encode($data);
            exit;
        }

        if(!$checkData){
            $data['statusCode'] = 2;
            $data['msg'] = '请先完善基本资料';
            echo json_encode($data);
            exit;
        }

        $checkApply = $this->Mapplyinterview->checkApply($id);
        if(!$checkApply){
            $data['statusCode'] = 2;
            $data['msg'] = '已加入，不能重复加入';
            echo json_encode($data);
            exit;
        }
        $this->Mapplyinterview->insertApply($id,$realname,$weixin);
        $this->Mweibo->saveWeibo($id,$wid,$weixin);
        $this->Muser->updateUserRealName($id,$realname);

        $data['statusCode'] = 1;
        $data['msg'] = '报名成功';
        echo json_encode($data);
    }

    function girl()
    {
        $data['girl'] = $this->Mgirl->getGirlAll(0,10);

        $data['title'] = '人气女郎';
        $data['starMethod'] = 'girl';
        $this->load->view('girl',$data);
    }





}